<?php
/**
 * Created by PhpStorm.
 * User: Qiangguo
 * Date: 14-9-11
 * Time: 上午9:57
 */
namespace Home\Controller;
use Think\Controller;
class ActivityController extends Controller {


    //跳转到添加活动页面时先查看数据库里是否有已保存未提交的活动。分别对cost、preoare、activity进行查找
    //判断条件：
    public function preToAct(){

   
        //$uid = $_SESSION['uid']
        $uid=1;
        //遍历activity表如果有aid说明有未完成的记录，根据aid去遍历cost和prepare和picture
        $aid=self::allActivity($uid);
        if ($aid==null) {
            //说明没有保存未提交的活动
             $this->assign("msg",2);
             $this->assign('costNum',3);
             $this->assign('prepareNum',3);
        }else{
            //$this->assign("msg","您有保存未提交的活动。请提交后进行活动添加");
            $this->assign("msg",1);
            self::allCost($aid);
            self::allPrepare($aid);
            self::allPicture($aid);
        }

        if( $_GET['msg']==3){

            $this->assign("msg",3);
        }
        
        $this->display('addActivity');

    }

    //遍历activity库
    protected function allActivity($id){

        $activity = M("activity");
        $condition['pid'] = $id;
        $condition['astatus'] = 4;
        //同一个人保存的活动只能为一个
        $data = $activity->where($condition)->find();
       // print_r($data['aid']);
        $this->assign("activity",$data);
        return $data['aid'];
    }

    //驳回的活动遍历activity库
    protected function allRejectActivity(){

        //$uid = $_SESSION['uid']
        $uid=1;

        $activity = M("activity");
        $condition['pid'] = $uid;
        $condition['astatus'] = 4;
        //同一个人保存的活动只能为一个
        $data = $activity->where($condition)->find();
       // print_r($data['aid']);
        $this->assign("activity",$data);
        return $data['aid'];
    }



    //遍历cost库
    protected function allCost($id){
        $cost = M('cost');
        $condition['aid']=$id;
        /*$condition['pid'] = $id;
        $condition['cstatus'] = 4;*/
        $data = $cost->where($condition)->select();
        
         //判断是否有结果
        if(count($data)==0){
            $this->assign('costNum',3);
        }else{
            $this->assign('costNum',count($data));
        }
        $this->assign('cost',$data);
    }
    //遍历prepare库
    protected function allPrepare($id){
        $prepare = M("prepare");
        $condition['aid']=$id;
       /* $condition['pid'] = $id;
        $condition['pstatus'] = 4;*/
        $data = $prepare->where($condition)->select();
        //判断是否有结果
        if(count($data)==0){
            $this->assign('prepareNum',3);
        }else{
            $this->assign('prepareNum',count($data));
        }
        $this->assign('prepare',$data);
        
    }
    /*遍历图片*/

    protected function allPicture($id){

        $pic = M('Picture');
        $condition['aid']=$id;
        $data = $pic->where($condition)->select();
        if($data['path']!=null){
            /*说明保存过图片不在添加图片上传*/
            //print_r("fdggdsfgfd");
            $this->assign('havePic',1);
        }
    }
    //


    //ajax保存

    public function save(){
        //通过type判断是提交还是保存
        $status=$_GET['type'];
        $pid = 1;//session
        //添加活动
        $aid = $_POST['aid'];
        console.log($aid);
       // dump($fields);
        //第一次添加使用insert
        if($aid == null){

            $Activity = M('Activity');
            $Activity ->create();
            //修改数据为当前用户数据
            $Activity->pid = $pid;
            $Activity->astatus = $status;
            //返回aid用于准备和花费添加
            $aidTemp = $Activity->add(); 
            //添加准备，
            $Prepare = M('Prepare');
            //构造prepare数据
            $prepareData = array();
        
            foreach ($_POST['prtime'] as $key => $value) {
                $prepareData[$key]['prtime'] = $value == null ? null : $value;
            }
            foreach ($_POST['prcontent'] as $key => $value) {
                $prepareData[$key]['prcontent'] = $value == null ? null : $value;
               /* $prepareData[$key]['pstatus'] =$status;*/
                $prepareData[$key]['aid'] =$aidTemp;


                /* $prepareData[$key]['pid'] =$pid;//session*/
            }
            $Prepare ->addAll($prepareData);
            
            //添加花费
            $Cost = M('Cost');
            $costData = array();
            foreach ($_POST['ccategory'] as $key => $value) {
                $costData[$key]['ccategory'] = $value == null ? null : $value;
            }
            foreach ($_POST['ccontent'] as $key => $value) {
                $costData[$key]['ccontent'] = $value == null ? null : $value;
            }
            foreach ($_POST['cactual'] as $key => $value) {
                $costData[$key]['ccategory'] = $value == null ? null : $value;
            }
            foreach ($_POST['cost'] as $key => $value) {
                $costData[$key]['cost'] = $value == null ? null : $value;
               /* $costData[$key]['cstatus'] =$status;*/
                $costData[$key]['aid'] =$aidTemp;

              /*  $costData[$key]['pid'] =$pid;//sessdion*/

            }
            $Cost ->addAll($costData);


            /*添加图片*/

            $Pic = M('Picture');
            $picData = array();
            print_r($_POST['path']);
            foreach ($_POST['path'] as $key => $value) {
                $picData[$key]['path'] = $value == null ? null : $value;
                $picData[$key]['aid'] =$aidTemp;
            }
            $Pic ->addAll($picData);


            //print_r($Prepare);  
        }else{
            //保存后再次保存使用update
            $Activity = M('Activity');
            $Activity ->create();
            //修改数据为当前用户数据
           
            $Activity->pid =$pid;
            $Activity->astatus = $status;
            //返回aid用于准备和花费添加
            $Activity->save(); 
            //添加准备，
            $Prepare = M('Prepare');
            //构造prepare数据
            $prepareData = array();
        
            foreach ($_POST['prid'] as $key => $value) {
                $prepareData[$key]['prid'] = $value == null ? null : $value;
            } 
            foreach ($_POST['prtime'] as $key => $value) {
                $prepareData[$key]['prtime'] = $value == null ? null : $value;
            }
            foreach ($_POST['prcontent'] as $key => $value) {
                $prepareData[$key]['prcontent'] = $value == null ? null : $value;
               /* $prepareData[$key]['pstatus'] =$status;*/
                $prepareData[$key]['aid'] =$aid;

             /*   $prepareData[$key]['pid'] =$pid;//session*/
            }
            $Prepare ->addAll($prepareData,$options,true);

            //dump($prepareData);
           //添加花费
            $Cost = M('Cost');
            $costData = array();

            foreach ($_POST['cid'] as $key => $value) {
                $costData[$key]['cid'] = $value == null ? null : $value;
            }
            foreach ($_POST['ccategory'] as $key => $value) {
                $costData[$key]['ccategory'] = $value == null ? null : $value;
            }
            foreach ($_POST['ccontent'] as $key => $value) {
                $costData[$key]['ccontent'] = $value == null ? null : $value;
            }
            foreach ($_POST['cactual'] as $key => $value) {
                $costData[$key]['cactual'] = $value == null ? null : $value;
            }
            foreach ($_POST['cost'] as $key => $value) {
                $costData[$key]['cost'] = $value == null ? null : $value;
               /* $costData[$key]['cstatus'] =$status;*/
                $costData[$key]['aid'] =$aid;

                /*$costData[$key]['pid'] =$pid;//sessdion*/
            } 
            $Cost ->addAll($costData,$options,true);
           // dump($costData);

             /*添加图片*/

            $Pic = M('Picture');
            $picData = array();
            foreach ($_POST['path'] as $key => $value) {
                $picData[$key]['path'] = $value == null ? null : $value;
                $picData[$key]['aid'] =$aid;
            }
            $Pic ->addAll($picData,$options,true);

        }
        
        if($status==1){
           
           $this->redirect("/Home/Activity/preToAct", array('msg' => 3));
        }
        elseif ($status==4) {

            $this->ajaxReturn($aidTemp);
        }
    }
   //ajax保存照片
    public function savePic(){

       if (!empty($_FILES)) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            // 上传文件 
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                    echo($upload->getError());
            }else{// 上传成功
                 //$this->success('上传成功！');
               foreach($info as $file){
                    echo json_encode($file);
                }
            }
        }
       
    }
    //删除照片
    public function delPic(){

        $path=$_POST['savepath'];
        $name=$_POST['savename'];
        $pic = M('Picture');
        $condition['path']=$path.$name;
        $data = $pic->where($condition)->delete();
        if(unlink('./Public/upload/'.$path.$name)){
            echo "delok";
        }else{
            echo "error";
        }

        
    }
    //获取所有的已通过的活动进行报名
    protected function allAllowActivity(){
        $activity = M("activity");
        $condition['astatus'] = 2;
        $data = $activity->where($condition)->order('atime desc')->select();

        $this->assign("act_list",$data);
    }

    //获取所有的已通过的活动进行报名
    protected function allSubmitActivity(){
        $activity = M("activity");
        $condition['astatus'] = 1;
        $data = $activity->where($condition)->order('atime desc')->select();
        $this->assign("act_list",$data);
    }

    public function index(){
        $this->display('Activity:addActivity');
    }
    public function regActivity(){
        $this->allAllowActivity();

        $this->display('Activity:regActivity');
    }
    public function mngActivity(){
        $this->allSubmitActivity();

        $this->display('Activity:mngActivity');
    }
    public function addActivity(){


    }
    public function getActivity($aid){
        $activity = M("activity");
        $condition['aid'] = $aid;
        $data = $activity->where($condition)->find();
        $this->assign("activity",$data);
        $stu_list = [];
        $activitycount = M("activitycount");
        $datalist = $activitycount->where($condition)->select();
        $user = M("user");
        foreach($datalist as $data){
            $user_condition['userid'] = $data['pid'];
            $user_data = $user->where($user_condition)->find();
            $array = array("acid" => $data['acid'],
                "aid" => $data['aid'],
                "pid" =>$data['pid'],
                "actime" => $data['actime'],
                "acexperience" =>$data['acexperience'],
                "acway" => $data['acway'],
                "acreferee" => $data['acreferee'],
                "userid" =>$user_data['userid'],
                "username" =>$user_data['username']
            );
            array_push($stu_list, $array);
        }
        $this->assign("stu_list",$stu_list);
        $this->display('Activity:detailActivity');
    }
    public function updateActivity(){}
    public function searchActivity(){}
    //删除呗
    public function deleteActivity($aid){
       //delete;
        $Activity = M("activity"); //
        $map['aid'] = $aid;
        $Activity->where($map)->delete();
        $this->mngActivity();
    }

    //通过活动
    public function allowActivity($aid){
        //通过;
        $Activity = M("activity"); //
        //
        $data['astatus'] = 2;
        $data['backup'] ="";
        $map['aid'] = $aid;
        $Activity->where($map)->save($data); // 根据条件更新记录
        $this->mngActivity();
    }

    //驳回活动
    public function rejectActivity($aid){

        //驳回;
        $Activity = M("activity"); //
        //
        $data['astatus'] = 3;
        $map['aid'] = $aid;
        $data['backup'] = $_POST['backup'];

        $Activity->where($map)->save($data); // 根据条件更新记录

        $this->mngActivity();
    }



}

